﻿// Auto upload file using jquery uploadify
// Handle 3 action upload at setup wizard view
// Apply css class, action to get data after import file
var errMessageInvalidFile = 'Only xls, xlsx, or csv files are allowed or upload file format is invalid.';
var uploadStatus = "nothing";
$(function () {
    // First file
    $("#fileData").uploadify({
        'multi': false,
        'buttonText': 'Upload Users',
        'buttonClass': 'btn btn-upload-users',
        'width': 105,
        'height': 40,
        'swf': '/images/uploadify.swf',
        'fileTypeDesc': 'Excel Sheet',
        'fileTypeExts': '*.xls; *.xlsx; *.csv',
        'scriptData': { 'token': tokenValue },
        'uploader': '/SetupWizard/Upload/?token=' + tokenValue,
        'onUploadProgress': function (file) {
            // Disabler
            if (uploadStatus == "nothing") {
                closePopup();
                openProcess();
                uploadStatus = "uploading";
            }
        },
        'successTimeout': 600,
        'onUploadSuccess': function (file, data, response) {
            //console.log("fileData11111111");
            hideProcess();
            var data = jQuery.parseJSON(data);
            uploadStatus = "nothing";
            if (data != null) {
                if (data.Error == undefined || data.Error == null || data.Error == false) {
                    if (data.AllowAddAgent || data.AllowAddAgent == undefined) {
                        UpdateData(data);
                    }
                    else {
                        ShowTooManyUserPopup(data);

                    }
                }
                else {
                    if (data.WrongColumn && data.UserList.length > 0) {     // Has wrong some fields
                        UpdateData(data);
                    }
                    else {  // data.Error = true : Invalid format file, Invalid template file
                        if (data.ItemSuccess != undefined && data.ItemSuccess != null)
                            showAlertPopup(errMessageInvalidFile, '');
                        else
                            showPopupError();
                    }
                }
            }
            else {
                showPopupError();
            }
        },
        'onUploadError': function (event, errorCode, fileObj, errorObj) {
            uploadStatus = "nothing";
            hideProcess();
            showUploadError();
            if (typeof console != "undefined" && typeof console.log != "undefined") {
                console.log("error code: " + errorCode);
                console.log("error object: " + errorObj);
            }
        }
    });

    // Second file

    $("#fileData2").uploadify({
        'multi': false,
        'buttonText': 'Upload Users',
        'buttonClass': 'btn btn-upload-again',
        'width': 105,
        'height': 40,
        'swf': '/images/uploadify.swf',
        'fileTypeDesc': 'Excel Sheet',
        'fileTypeExts': '*.xls; *.xlsx; *.csv',
        'uploader': '/SetupWizard/Upload?token=' + tokenValue,
        'onUploadProgress': function (file) {
            // Disabler
            if (uploadStatus == "nothing") {
                closePopup();
                openProcess();
                uploadStatus = "uploading";
            }
        },
        'successTimeout': 600,
        'onUploadSuccess': function (file, data, response) {
            uploadStatus = "nothing";
            hideProcess();
            var data = jQuery.parseJSON(data);
            if (data != null) {
                if (data.Error == undefined || data.Error == null || data.Error == false) {
                    if (data.AllowAddAgent || data.AllowAddAgent == undefined) {
                        UpdateData(data);
                    }
                    else {
                        ShowTooManyUserPopup(data);
                    }
                }
                else {
                    if (data.WrongColumn && data.UserList.length > 0) {     // Has wrong some fields
                        UpdateData(data);
                    }
                    else {  // data.Error = true : Invalid format file, Invalid template file
                        if (data.ItemSuccess != undefined && data.ItemSuccess != null)
                            showAlertPopup(errMessageInvalidFile, '');
                        else
                            showPopupError();
                    }
                }
            }
            else {
                showPopupError();
            }
        },
        'onUploadError': function (event, errorCode, fileObj, errorObj) {
            uploadStatus = "nothing";
            hideProcess();
            showUploadError();
            if (typeof console != "undefined" && typeof console.log != "undefined") {
                console.log("error code: " + errorCode);
                console.log("error object: " + errorObj);
            }
        }
    });

    // Three
    $("#fileData3").uploadify({
        'multi': false,
        'buttonText': 'Upload Users',
        'buttonClass': 'btn btn-upload-again',
        'width': 105,
        'height': 38,
        'swf': '/images/uploadify.swf',
        'fileTypeDesc': 'Excel Sheet',
        'fileTypeExts': '*.xls; *.xlsx; *.csv',
        'uploader': '/SetupWizard/Upload?token=' + tokenValue,
        'onUploadProgress': function (file) {
            // Disabler
            if (uploadStatus == "nothing") {
                closePopup();
                openProcess();
                uploadStatus = "uploading";
            }
        },
        'successTimeout': 600,
        'onUploadSuccess': function (file, data, response) {
            hideProcess();
            //console.log("fileData3333333333333");
            var data = jQuery.parseJSON(data);
            uploadStatus = "nothing";
            if (data != null) {
                if (data.Error == undefined || data.Error == null || data.Error == false) {
                    if (data.AllowAddAgent || data.AllowAddAgent == undefined) {
                        UpdateData(data);
                        // Stop process
                        $('#progress3').html("");
                    }

                    else {
                        ShowTooManyUserPopup(data);
                    }
                }
                else {
                    if (data.WrongColumn && data.UserList.length > 0) {     // Has wrong some fields
                        UpdateData(data);
                    }
                    else {  // data.Error = true : Invalid format file, Invalid template file
                        if (data.ItemSuccess != undefined && data.ItemSuccess != null)
                            showAlertPopup(errMessageInvalidFile, '');
                        else
                            showPopupError();
                    }
                }
            }
            else {
                showPopupError();
            }
        },
        'onUploadError': function (event, errorCode, fileObj, errorObj) {
            uploadStatus = "nothing";
            hideProcess();
            showUploadError();
            if (typeof console != "undefined" && typeof console.log != "undefined") {
                console.log("error code: " + errorCode);
                console.log("error object: " + errorObj);
            }
        }
    });
});


function ShowTooManyUserPopup(data) {
    var message = "Cannot add more users.  You have used all available seat licenses.";
    $('#itemName').text(message);

    $('#alertPopup').showPopup({
        width: 480
    });
    $('#fancybox-overlay').show();
}

function UpdateData(data) {
    yourTeamModel.WrongVisible(false);
    //var data = jQuery.parseJSON(data);
    if (data.Error == true) {
        yourTeamModel.DisplayPanel(false);
        yourTeamModel.DisplayPanelError(true);
        yourTeamModel.DisplayPanelSuccess(false);
    }
    else {
        yourTeamModel.DisplayPanel(false);
        yourTeamModel.DisplayPanelError(false);
        yourTeamModel.DisplayPanelSuccess(true);
    }

    // Set value again
    if (data.UserList != undefined && data.UserList != null && data.UserList.length > 0) {
        yourTeamModel.ValidateDisplayTable(true);
        yourTeamModel.TotalUserSuccess(data.UserList.length);
        yourTeamModel.Users(data.UserList);
        //console.log("----------------------");
        //console.log(data);
        yourTeamModel.WrongVisible(data.Error);
        yourTeamModel.TotalUsers(data.TotalUser);
        yourTeamModel.numberOfUsers(data.TotalUserAdded);
        if (data.Error == true) {
            AddBorderRightClass();
        }
        else {
            RemoveBorderRightClass();
        }
    }
    else {
        yourTeamModel.ValidateDisplayTable(false);
        yourTeamModel.Users([]);
    }
    FixBorderTableClass();
    fix_table_cols_height_tableStep1($("#listUsers"));
}
